package com.pool.activity;

import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.pool.R;
import com.pool.activity.common.FullscreenListActivity;
import com.pool.common.Lists;
import com.pool.common.Maps;
import com.pool.common.Record;
import com.pool.common.Shoot;
import com.pool.store.RecordStore;
import com.pool.store.ShootStore;
import com.pool.store.Store;

/**
 * @author claveld@gmail.com (David Clavel)
 */
public class Statistics extends FullscreenListActivity {

  private static final String[] PLAYERS = {"David", "Mateusz"};
  
  private final Store<Record> permanentRecordStore;
  private final Store<Record> temporaryRecordStore;
  private final Store<Shoot> shootStore;

  public Statistics() {
    this(RecordStore.usingPermanentTable(),
         RecordStore.usingTemporaryTable(),
         new ShootStore());
  }
  
  public Statistics(Store<Record> permanentRecordStore,
                    Store<Record> temporaryRecordStore, 
                    Store<Shoot> shootStore) {
    this.permanentRecordStore = permanentRecordStore;
    this.temporaryRecordStore = temporaryRecordStore;
    this.shootStore = shootStore;
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.local_store);

    List<Map<String, Object>> myData = Lists.newArrayList();
    String[] statsForPlayers = getStatsFor(PLAYERS);
    
    for (int i = 0; i < PLAYERS.length; i++) {
      addItem(myData, PLAYERS[i], statsForPlayers[i], History.createIntentWithFilter(this, PLAYERS[i]));
    }

    // Separator
    addItem(myData, "", "");
    
    addItem(myData, "Full history", "All-time results for both players.", History.createIntentWithFilter(this, ""));
    addItem(myData, "Graph", "Displays points over time for both players.", new Intent(this, PointsOverTime.class));
    
    setListAdapter(new SimpleAdapter(
        this,
        myData,
        R.layout.two_line_row,
        new String[] { "line1", "line2" },
        new int[] { R.id.text1, R.id.text2 }));
    getListView().setTextFilterEnabled(true);
    getListView().setDividerHeight(2);
  }

  @SuppressWarnings("unchecked")
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
    Intent intent = (Intent) map.get("intent");
    if (intent != null) {
      startActivity(intent);
    }
  }

  private void addItem(List<Map<String, Object>> list, String line1, String line2) {
    addItem(list, line1, line2, null);
  }

  private void addItem(List<Map<String, Object>> list, String line1, String line2, Intent intent) {
    Map<String, Object> map = Maps.newHashMap();
    map.put("line1", line1);
    map.put("line2", line2);
    map.put("intent", intent);
    list.add(map);
  }

  private String[] getStatsFor(String[] players) {
    int numberOfPlayers = players.length;
    int[] victories = new int[numberOfPlayers];
    int[] points = new int[numberOfPlayers];
    
    Map<String, Integer> playerToIndex = Maps.newHashMap();
    for (int i = 0; i < numberOfPlayers; i++) {
      Log.i("INFO", "Storing " + players[i] + " with index " + i);
      playerToIndex.put(players[i], i);
      victories[i] = 0;
      points[i] = 0;
    }
    
    Map<String, Shoot> shootsPerName = getShootsPerName();
    
    List<Store<Record>> stores = Lists.newArrayList(2);
    stores.add(permanentRecordStore);
    stores.add(temporaryRecordStore);
    for (Store<Record> store : stores) {
      for (Record record : store.getAll()) {
        int index = playerToIndex.get(record.player);
        if (isVictory(record)) {
          victories[index]++;
        }
        points[index] += shootsPerName.get(record.shootId).bonus;
      }
    }

    String[] statsPerPlayer = new String[numberOfPlayers];
    for (int i = 0; i < numberOfPlayers; i++) {
      statsPerPlayer[i] = victories[i] + " victories\n" + points[i] + " points";
    }
    return statsPerPlayer;
  }
  
  private boolean isVictory(Record record) {
    return Shoot.isVictory(record.shootId);
  }
  
  private Map<String, Shoot> getShootsPerName() {
    List<Shoot> shoots = shootStore.getAll();
    Map<String, Shoot> shootsPerName = Maps.newHashMap();
    for (Shoot shoot : shoots) {
      shootsPerName.put(shoot.id, shoot);
    }
    return shootsPerName;
  }
}
